<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);
pc_base::load_sys_class('form','',0);

/**
 * 留言模块
 */
class preview extends admin {
	private $epaperDb;
	public $siteid;
	public $catid = 7;
	function __construct() {
        parent::__construct();
		$this->epaperDb = pc_base::load_model('content_model');
		$this->epaperDb->set_catid($this->catid);
		$this->siteid = $this->get_siteid();
	}
	
	// 显示某期的四张图片
	public function preview() {

		$date = $_GET['date']??'';
		$date = safe_replace($date);
		if(!$date)
			showmessage('发生错误 date 不能为空');

		$data = $this->epaperDb->select(['date'=>$date], 'page,page_name,page_image,date', '', 'page asc', 'page');
		if(!$data)
			showmessage('发生错误：no exists');

		include $this->admin_tpl('preview');
    }

	// 替换图片
	public function image_update() {
		// 解析 json 数据
		$thumb = $_POST['image']??'';
		$thumb = safe_replace($thumb);
		if(!$thumb)
			throw new Exception('thumb地址错误');

		// 解析地址
		$urls = parse_url($thumb);
		$thumb = $urls['path'];
		$thumb = trim($thumb, "/");

		// 获取上传的文件地址
		$tmp_path = $_FILES['file']['tmp_name']??null;
		if(!$tmp_path){
			header("HTTP/1.1 400");
			echo '文件上传错误';
			return;
		}

		// 文件后缀验证
		$name = $_FILES['file']['name']??null;
		$ext = end(explode('.', $name));
		$allows = ['jpg','jpeg','webm','png'];

		if(!in_array(strtolower($ext),$allows)){
			header("HTTP/1.1 400");
			echo '文件类型非法';
			return;
		}

		// 验证是否是post文件
		if(!is_uploaded_file($tmp_path)){
			header("HTTP/1.1 400");
			echo '非法文件';
			return;
		}

		if(!file_exists($thumb)){
			header("HTTP/1.1 500");
			echo '非法操作';
			return;
		}

		if(!move_uploaded_file($tmp_path, $thumb)){
			header("HTTP/1.1 500");
			echo '修改失败';
			return;
		}

		echo '修改成功';
		return;
    }

	// 文章列表页预览
	public function get_content() {

		$id = $_GET['id'];

		$epaper_id = safe_replace($id);
	
		$db = pc_base::load_model('content_model');
		$db->set_catid(CAT_ID);
	
		$r = $db->get_one(['id'=>$epaper_id], 'id,title,title2,description,intro_title,sub_title,author,page,page_name,date');
		if(!$r)
			die('[]');
	
		// 获取某天的热区数据 data 表格
		$table_name = $db->table_name;
		$db->table_name = $db->table_name.'_data';
		$rd = $db->get_one(['id'=>$epaper_id], 'id,image,content,relation,audio');
	
		// 连接转为全连接
		// $rd['content'] = str_replace('src="uploadfile','src="https://lab.chinaluan.com/uploadfile',$rd['content']);
		// &nbsp; 转为 英文空格
		// $rd['content'] = str_replace('&nbsp; '," ",$rd['content']);
		// 13px margin-top
		// $rd['content'] = str_replace('<p>','<p style="margin-top:13px;">',$rd['content']);
	
	
	 
		$rt = array_merge($r,$rd);
		// 推荐新闻
		$relation_arr = [];
		$relation_ids = array_filter(explode("|", $rt['relation']));
		if($relation_ids){
			$db->table_name = $table_name;
			$relation_arr = $db->select('id in ('.implode(',',$relation_ids).')', "id,title2,intro_title,sub_title,thumb,author,page,page_name,date");
		}
	
		$rt['relation'] = $relation_arr;

		header("Content-type:application/json;charset=utf-8");
		echo json_encode($rt, JSON_UNESCAPED_UNICODE);
	}


}
